package net.ulrice.mdls.richclient;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import net.ulrice.Ulrice;
import net.ulrice.appprefs.IFAppPrefs;
import net.ulrice.configuration.ConfigurationException;
import net.ulrice.configuration.DefaultUlriceConfiguration;
import net.ulrice.configuration.UlriceConfigurationCallback;
import net.ulrice.frame.impl.MainFrameConfig;
import net.ulrice.frame.impl.Menubar;
import net.ulrice.frame.impl.Toolbar;
import net.ulrice.mdls.richclient.monitor.MonitorModule;
import net.ulrice.mdls.services.Application;
import net.ulrice.mdls.services.MdlsService;
import net.ulrice.mdls.services.Settings;
import net.ulrice.module.ModuleRegistrationHelper;
import net.ulrice.module.impl.ModuleGroup;
import net.ulrice.module.impl.action.CloseAllModulesAction;
import net.ulrice.module.impl.action.CloseModuleAction;
import net.ulrice.module.impl.action.ExitApplicationAction;
import net.ulrice.module.impl.action.ModuleActionManager;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

/**
 * Main class for the mdls rich client.
 * 
 * @author christof
 */
public class Richclient {

	private static MdlsService.Client mdlsService;

	/**
	 * Load the settings, initialize and show ulrice.
	 * @throws TException 
	 */
	public static void main(String[] args) throws ConfigurationException, TException {
		StartupDialog.showDialog();
	}

	public static void connect(String serverUrl) throws ConfigurationException, TException {
		initializeThrift(serverUrl);
		System.out.println(mdlsService.getMdlsVersion());
		
		
		Ulrice.start(new DefaultUlriceConfiguration(new UlriceConfigurationCallback() {
			
			@Override
			public void configureUlrice(IFAppPrefs appPrefs, MainFrameConfig mainFrameConfig) {
				mainFrameConfig.setShowInstanceTree(false);
				mainFrameConfig.setToolbarActionOrder(ExitApplicationAction.ACTION_ID, Toolbar.SEPARATOR, Toolbar.MODULE_ACTIONS);
				mainFrameConfig.setTitle("Meltdown Loadtest Suite");
			}

			@Override
			public void addApplicationActions(ModuleActionManager moduleActionManager) {
				moduleActionManager.addApplicationAction(new CloseModuleAction("Close Module", new ImageIcon(Richclient.class.getResource("close.gif"))));
				moduleActionManager.addApplicationAction(new CloseAllModulesAction("Close all Modules", null));
				moduleActionManager.addApplicationAction(new ExitApplicationAction("Exit", new ImageIcon(Richclient.class.getResource("exit.png"))));	
			}

			@Override
			public void configureMenu(Menubar menubar, ModuleActionManager actionManager) {
				JMenu file = new JMenu("File");
				file.add(new JMenuItem(actionManager.getApplicationAction(CloseModuleAction.ACTION_ID)));
				file.add(new JMenuItem(actionManager.getApplicationAction(CloseAllModulesAction.ACTION_ID)));		
				file.add(new JMenuItem(actionManager.getApplicationAction(ExitApplicationAction.ACTION_ID)));				
				menubar.add(file);
			}

			@Override
			public void registerModules(ModuleRegistrationHelper regHelper) {
				
				
				ModuleGroup monitorGroup = new ModuleGroup(1, "MONITOR", "Monitor", null, true);
				
				Settings settings;
				try {
					settings = mdlsService.loadSettings();		
					regHelper.addGroup(monitorGroup);
					for(Application application : settings.getApplications()) {
						regHelper.addModuleToGroup(monitorGroup, new MonitorModule(application));
					}
				} catch (TException e) {
					e.printStackTrace();
				}		
			}			
		}));
	}

	private static void initializeThrift(String serverUrl) throws TTransportException {
		String[] split = serverUrl.split(":");
		TTransport transport = new TSocket(split[0], Integer.parseInt(split[1]));
		transport.open();

		TProtocol protocol = new  TBinaryProtocol(transport);
		mdlsService = new MdlsService.Client(protocol);
	}
}
